package com.prs.crm.action;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.courser.Util;
import org.courser.struts.XJAction;

import com.prs.JohnLan.OutPutEnpassword;
import com.prs.crm.context.AppContext;
import com.prs.crm.context.UserLoginStore;

import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.sys.Menu;

import com.prs.crm.service.exchange.OnlinePersonService;
import com.prs.crm.service.hr.PersonService;

public class LoginAction extends XJAction implements ServletRequestAware {

	private static final long serialVersionUID = 1783747748661489585L;

	private PersonService personService;

	private OnlinePersonService onlinePersonService;
	private int ch = 0;
	private HttpServletRequest servletRequest;

	private String username;

	private String password;




	@Action(value = "login", results = {
			@Result(name = "success", type = "redirect", location = "home"),
			@Result(name = "error", location = "/index.ftl") })
	public String login() {
			String pass=OutPutEnpassword.outPassword(password);
			if (personService.login(username, pass)) {
				this.getOnlinePersonService().saveLogin(
						AppContext.getAccountContext().getUser(),
						Util.getRemoteIp(servletRequest));
				initRole();
				
				return SUCCESS;
			} else {
				this.addActionError("用户名或密码错误");
				return "error";
			}
	
	}

	private void initRole() {
		Person account = AppContext.getAccountContext().currentUser();
		UserLoginStore.putUser(account);
		if (account.getRole() != null && account.getRole().getMenus() != null) {
			Map<String, Menu> map = new HashMap<String, Menu>();
			for (Menu menu : account.getRole().getMenus()) {
				map.put(menu.getId().toString(), menu);
			}
			this.getStorage().save("com.prs.crm.store.role", map);
		}
	}


	
	public PersonService getPersonService() {
		return personService;
	}

	public  void setPersonService(PersonService personService) {
		this.personService = personService;
	}
	
	



	public OnlinePersonService getOnlinePersonService() {
		return onlinePersonService;
	}

	public void setOnlinePersonService(OnlinePersonService onlinePersonService) {
		this.onlinePersonService = onlinePersonService;
	}

	public int getCh() {
		return ch;
	}

	public void setCh(int ch) {
		this.ch = ch;
	}

	public HttpServletRequest getServletRequest() {
		return servletRequest;
	}

	public void setServletRequest(HttpServletRequest servletRequest) {
		this.servletRequest = servletRequest;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}


	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	

}
